package com.pad.dpscn.daag.sdk.core.strategy.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.pad.dpscn.daag.sdk.core.strategy.MessageQueueStrategy;
/**
 * 
 * com.pad.dpscn.daag.sdk.core.strategy.service  
 *  
 * <p>  
 * Description: 消息发送策略  
 * </p>  
 *  
 * @author yangdezhao
 * @since 2024年3月11日  
 * @version 1.0
 */
@Component
public class MessageQueueService {
	private static final Log log = LogFactory.getLog(MessageQueueService.class);

	private final Map<String, MessageQueueStrategy> strategies = new ConcurrentHashMap<>();
	private String topicName;

	public String getTopicName() {
		return topicName;
	}

	public MessageQueueService(String topicName) {
		super();
		this.topicName = topicName;
	}

	public void registerStrategy(String type, MessageQueueStrategy strategy) {
		strategies.put(type, strategy);
	}

	public void sendMessage(String type,String tag,String message) {
		MessageQueueStrategy strategy = strategies.get(type);
		if (strategy != null) {
			if(!StringUtils.isEmpty(topicName)) {
				String[] topicArray = topicName.split(":");
				String topicNameTag = topicArray.length>1 ? topicName : topicName+":"+tag;
				strategy.asyncSendAndReserveResult(topicNameTag, message);
			}
		} else {
			log.warn("No strategy registered for type: " + type);
		}
	}
	public void asyncSendMessage(String type, String message) {
		MessageQueueStrategy strategy = strategies.get(type);
		if (strategy != null) {
			strategy.asyncSendMessage(topicName, message);
		} else {
			log.warn("No strategy registered for type: " + type);
		}
	}
}
